package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Observer;

import Client.ObservableClient;
import Entities.ClientEntity;
import Entities.LoginEntity;
import GUI.ClientGUI;
import GUI.LoginGUI;
/**
 * ClientController class implements Validate interface
 * and creates the connection between client and sever.
 * @author Ofek-az
 *
 */
public class ClientController implements Validate{
	public static String uid;
	/**
	 * This is the client boundary.
	 */
	private ClientGUI clientGui;
	/**
	 * This is the client entity.
	 */
	private ClientEntity clientEnt;
	/**
	 * This is the observable client.
	 */
	public static ObservableClient observableClient;
	private LoginController loginController;
	public ClientController(ClientEntity clientEnt)
	{
		this.clientEnt=clientEnt;
	//	autoConnectToServer("localhost",4000);
	}
	/**
	 * This is the class constructor.
	 *
	 * @param clientGui This is the client boundary.
	 * @param clientEnt This is the client entity.
	 */
	public ClientController(ClientGUI clientGui,ClientEntity clientEnt)
	{
		this.clientGui = clientGui;
		this.clientEnt = clientEnt;
		this.clientGui.addConnectActionListener(new ClientStartListener());
	}
	
	/**
	 * This method changes the client observer.
	 *
	 * @param o new observer
	 */
	public static void setObserver(Observer o)
	{
		observableClient.deleteObservers();
		observableClient.addObserver(o);
	}
	/**
	 * ClientStartListener class implements ActionListener interface 
	 * this class controls the Connect button.
	 * @author Ofek-az
	 *
	 */
	class ClientStartListener implements ActionListener
	{
		
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			if(validateInput())
			{
				
				connectToServer();
			}
		}
	}
	
	
	
	/* (non-Javadoc)
	 * @see Controllers.Validate#validateInput()
	 */
	@Override
	public boolean validateInput() {
		String port=clientGui.getPortTf(),host=clientGui.getHostTf();
		if(port.equals("")||host.equals(""))
		{
			clientGui.displayWarningMessage("Please fill all missing fields.");
			return false;
		}
		else
		{
			try
			{
			Integer.parseInt(port);
			return true;
			}
			catch (NumberFormatException e)
			{
				
				clientGui.displayWarningMessage("Error:illeagel characters in port field.");
				return false;
			}
			
		}
	}
	public boolean autoConnectToServer(String host,int port)
	{
		clientEnt.setHost(host);
		clientEnt.setPort(port);
		observableClient=new ObservableClient(clientEnt.getHost(),clientEnt.getPort());
		setObserver(clientEnt);
		try {
			observableClient.openConnection();
				loginController=new LoginController(new LoginEntity());
		} catch (IOException e1) {
			System.out.println("Server is not connected");
			return false;
		}
		return true;
	}
	private void connectToServer()
	{
		clientEnt.setHost(clientGui.getHostTf());
		clientEnt.setPort(Integer.parseInt(clientGui.getPortTf()));
		observableClient=new ObservableClient(clientEnt.getHost(),clientEnt.getPort());
		setObserver(clientEnt);
		try {
			observableClient.openConnection();
			loginController=new LoginController(new LoginGUI(),new LoginEntity());
			clientGui.setVisible(false);
		} catch (IOException e1) {
			clientGui.displayWarningMessage("Server is not connected");
		}
	}
	public ClientGUI getClientGui() {
		return clientGui;
	}

	public void setClientGui(ClientGUI clientGui) {
		this.clientGui = clientGui;
	}

	public ClientEntity getClientEnt() {
		return clientEnt;
	}

	public void setClientEnt(ClientEntity clientEnt) {
		this.clientEnt = clientEnt;
	}

	public static ObservableClient getObservableClient() {
		return observableClient;
	}

	public static void setObservableClient(ObservableClient observableClient) {
		ClientController.observableClient = observableClient;
	}
	public LoginController getLoginController() {
		return loginController;
	}
	public void setLoginController(LoginController loginController) {
		this.loginController = loginController;
	}
	
}
